package com.ikun.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.ikun.dao.AttendancesDao;
import com.ikun.dao.BuildingsDao;
import com.ikun.dao.RoleUser;
import com.ikun.dao.UserDao;
import com.ikun.entity.Attendances;
import com.ikun.entity.Salaries;
import com.ikun.entity.Users;
import com.ikun.entity.req.MangerDTO;
import com.ikun.entity.req.OwnerDto;
import com.ikun.entity.req.StuffDTO;
import com.ikun.entity.req.UserDTO;
import com.ikun.service.SalariesService;
import com.ikun.service.UserService;
import com.ikun.util.DateUtil;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserDao userDao;

    @Autowired
    private BuildingsDao buildingsDao;

    @Autowired
    private AttendancesDao attendancesDao;

    @Autowired
    private SalariesService salariesService;

    @Autowired
    private RoleUser roleUser;

    @Override
    public Users findByUsername(String username) {
        return userDao.findByUsername(username);
    }

    @Override
    public List<Users> findAllOwner() {
        return userDao.findAllOwner();
    }

    @Override
    @Transactional // 开启事务保证两次操作必须一起完成
    public List<MangerDTO> findAllMangerDetail() {
        // 数据组装
        // 查询所有用户
        List<Users> users = userDao.findAll();
        List<MangerDTO> mangerDTOS = new ArrayList<>();
        String datePart = DateUtil.format(new Date(), "yyyy-MM");
        for (Users user : users) {
            Salaries salaries = salariesService.calculateSalary(datePart, user.getUserId().toString());
            // 查询每个用户的考勤信息
            mangerDTOS.add(new MangerDTO(user.getName(), user.getUserId(), null,
                salaries.getNetSalary().toString() == null ? "0" : salaries.getNetSalary().toString(),
                user.getCreatedTime() != null ? user.getCreatedTime().toString() : "",
                buildingsDao.findAllBuildingsByMangerId(user.getUserId()),
                attendancesDao.findAttendancesByMangerId(user.getUserId())));
        }
        return mangerDTOS;
    }

    @Override
    @Transactional // 开启事务保证两次操作必须一起完成
    public String updateUser(Users users) {
        userDao.deleteUser(users.getUserId());
        // todo 需判断用户是否插入成功
        int i = userDao.addUser(users);
        if (i == 0) {
            return "修改成功";
        } else {
            return "修改失败";
        }
    }

    @Override
    public String deleteUser(Integer userId) {
        int i = userDao.deleteUser(userId);
        if (i == 1) {
            return "删除成功";
        } else {
            return "删除失败";
        }
    }

    @Override
    @Transactional // 开启事务保证两次操作必须一起完成
    public String addManger(Users users) {
        // 添加管家
        int i = userDao.addUser(users);
        // 添加关联属性
        // 添加角色完成后会自动回填主键id users.getUserId()
        int r = roleUser.addRoleUser(users.getUserId(), 3);
        if (r == 1) {
            return "添加成功";
        } else {
            return "添加失败";
        }
    }

    @Override
    @Transactional // 开启事务保证两次操作必须一起完成
    public String addOwner(Users users) {
        // 添加管家
        int i = userDao.addUser(users);
        // 添加关联属性
        // 添加角色完成后会自动回填主键id users.getUserId()
        int r = roleUser.addRoleUser(users.getUserId(), 1);
        if (r == 1) {
            return "添加成功";
        } else {
            return "添加失败";
        }
    }

    @Override
    public String addStaff(Users users) {
        // 添加管家
        int i = userDao.addUser(users);
        // 添加关联属性
        int r = roleUser.addRoleUser(users.getUserId(), 2);
        if (r == 1) {
            return "添加成功";
        } else {
            return "添加失败";
        }
    }

    @Override
    public List<Attendances> findAllMangerAttendances(String datePart, String namePart) {
        return userDao.findAllMangerAttendances(datePart, namePart);
    }

    @Override
    public List<StuffDTO> findAllStuff() {
        List<Users> users = userDao.findAllStuff();
        List<StuffDTO> stuffDTOs = new ArrayList<>();
        for (Users user : users) {
            Salaries salaries =
                salariesService.calculateSalary(DateUtil.format(new Date(), "yyyy-MM"), user.getUserId().toString());
            stuffDTOs.add(new StuffDTO(user.getUserId(), user.getName(), null,
                salaries.getNetSalary().toString() == null ? "0" : salaries.getNetSalary().toString(),
                user.getCreatedTime() != null ? user.getCreatedTime().toString() : "",
                attendancesDao.findAttendancesByMangerId(user.getUserId())));
        }

        return stuffDTOs;
    }

    @Override
    public int countAllUsers() {
        return userDao.countAllOwners();
    }

    @Override
    public int countAllStuffs() {
        return userDao.countAllStuffs();
    }

    @Override
    public List<OwnerDto> findAllOwnerDetail() {
        return userDao.findAllOwnerDetail();
    }

    @Override
    public UserDTO findUserByName(String username) {
        // UserDTO userByName = userDao.findUserByName(username);
        // String roleName = userByName.getRoleName();
        // Map<String, Object> role = new HashMap<>();
        // if (roleName.equals("超级管理员")) {
        // return
        // }
        return userDao.findUserByName(username);
    }

    @Override
    public OwnerDto findAllOwnerDetailById(Integer id) {
        return userDao.findAllOwnerDetailById(id);
    }

    @Override
    public List<OwnerDto> findAllOwnerDetailByManagerId(Integer id) {
        return userDao.findAllOwnerDetailByManagerId(id);
    }

    @Override
    public List<OwnerDto> findAllOwnerInfoByManagerId(Integer id) {
        return userDao.findAllOwnerInfoByManagerId(id);
    }

    @Override
    public OwnerDto findAllOwnerInfoById(Integer id) {
        return userDao.findAllOwnerInfoById(id);
    }

    @Override
    public int updateOwner(String ownerId, String phone, String photoUrl) {
        return userDao.updateOwner(ownerId, phone, photoUrl);
    }

    @Override
    public List<UserDTO> findAllStaff() {
        return userDao.findAllStaff();
    }

    @Override
    public String updateInfo(String id, String phone, String password) {
        if (userDao.updateInfo(id, phone, password) > 0) {
            return "修改成功";
        } else {
            return "修改失败";
        }
    }

}
